home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / Map.h < prev    next >
C/C++ Source or Header  |  1996-12-06  |  3KB  |  163 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. /*
  24.  
  25. The classes in this file are derived from the old `genclass' versions
  26. of Map and CHMap from libg++, originally:
  27.  
  28.   Copyright (C) 1988 Free Software Foundation
  29.     written by Doug Lea (dl@rocky.oswego.edu)
  30.  
  31. and distributed under the terms of the GNU Library General Public
  32. License as published by the Free Software Foundation.
  33.  
  34. */
  35.  
  36. #if ! defined (octave_Map_h)
  37. #define octave_Map_h 1
  38.  
  39. #if defined (__GNUG__)
  40. #pragma interface
  41. #endif
  42.  
  43. #include <string>
  44.  
  45. #include <Pix.h>
  46.  
  47. template <class C>
  48. class Map
  49. {
  50. protected:
  51.   int count;
  52.   C def;
  53.  
  54. public:
  55.   Map (const C& dflt) : def (dflt) { count = 0; }
  56.  
  57.   virtual ~Map (void) { }
  58.  
  59.   int length (void) const { return count; }    // current number of items
  60.   int empty (void) const { return count == 0; }
  61.  
  62.   virtual int contains (const string& key) const;  // is key mapped?
  63.  
  64.   virtual void clear (void);            // delete all items
  65.           
  66.   virtual C& operator [] (const string& key) = 0;  // access contents by key
  67.           
  68.   virtual void del (const string& key) = 0;    // delete entry
  69.           
  70.   virtual Pix first (void) const = 0;        // Pix of first item or 0
  71.   virtual void next (Pix& i) const = 0;        // advance to next or 0
  72.   virtual string key (Pix i) const = 0;        // access key at i
  73.   virtual C& contents (Pix i) const = 0;    // access contents at i
  74.  
  75.   virtual int owns (Pix i) const;        // is i a valid Pix  ?
  76.   virtual Pix seek (const string& key) const;    // Pix of key
  77.  
  78.   C& dflt (void) { return def; }        // access default val
  79.  
  80.   void error (const string& msg) const;
  81.  
  82.   virtual int OK (void) const = 0;        // rep invariant
  83. };
  84.  
  85. template <class C>
  86. struct CHNode
  87. {
  88.   CHNode *tl;
  89.   string hd;
  90.   C cont;
  91.  
  92.   CHNode (void) : tl (0), hd (), cont () { }
  93.  
  94.   CHNode (const string& h, const C& c, CHNode *t = 0)
  95.     : tl (t), hd (h), cont (c) { }
  96.  
  97.   ~CHNode (void) { }
  98. };
  99.  
  100. #ifndef DEFAULT_INITIAL_CAPACITY
  101. #define DEFAULT_INITIAL_CAPACITY 8
  102. #endif
  103.  
  104. template <class C>
  105. class CHMap : public Map<C>
  106. {
  107. protected:
  108.   CHNode<C> **tab;
  109.   unsigned int size;
  110.  
  111. public:
  112.   CHMap (const C& dflt, unsigned int sz = DEFAULT_INITIAL_CAPACITY);
  113.  
  114.   CHMap (const CHMap& a);
  115.  
  116.   ~CHMap (void)
  117.     {
  118.       clear ();
  119.       delete tab;
  120.     }
  121.  
  122.   C& operator [] (const string& key);
  123.  
  124.   void del (const string& key);
  125.  
  126.   Pix first (void) const;
  127.   void next (Pix& i) const;
  128.  
  129.   string key (Pix p) const
  130.     {
  131.       if (p == 0)
  132.     error ("null Pix");
  133.  
  134.       return ((CHNode<C> *) p)->hd;
  135.     }
  136.  
  137.   C& contents (Pix p) const
  138.     {
  139.       if (p == 0)
  140.     error ("null Pix");
  141.  
  142.      return ((CHNode<C> *) p)->cont;
  143.    }
  144.  
  145.   Pix seek (const string& key) const;
  146.  
  147.   int contains (const string& key) const
  148.     {
  149.       return seek (key) != 0;
  150.     }
  151.  
  152.   void clear (void);
  153.   int  OK (void) const;
  154. };
  155.  
  156. #endif
  157.  
  158. /*
  159. ;;; Local Variables: ***
  160. ;;; mode: C++ ***
  161. ;;; End: ***
  162. */
  163.